﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DAL;
using BLL;
using BOL;
using DevComponents.DotNetBar;
using DevComponents.AdvTree;

namespace QuanLyHangXeKhach
{
    public partial class frmCapNhatLichChay : UserControl
    {
        CapNhatLichChay cnlc = new CapNhatLichChay();
        CapNhatGiaTuyen cngt = new CapNhatGiaTuyen();
        CapNhatNhanVienPhuTrachXe cnnvpt = new CapNhatNhanVienPhuTrachXe();
        int huy = 0;
        DieuKhien dk = new DieuKhien();

        public frmCapNhatLichChay()
        {
            InitializeComponent();
        }

        private void frmCapNhatLichChay_Load(object sender, EventArgs e)
        {
            txttimkiem.WatermarkText = "Tìm kiếm";

            cobtuyenduong.DataSource = cngt.laytuyen();
            cobtuyenduong.DisplayMember = "ten";
            cobtuyenduong.ValueMember = "ma";

            cobxe.DataSource = cnnvpt.layxe();
            cobxe.DisplayMember = "bienxe";
            cobxe.ValueMember = "maxe";

            xoatrang();
            themsua(false);
            Loadfrm();
        }

        public void xoatrang()
        {
            cobtuyenduong.Text = "";
            cobxe.Text = "";
            maskngayad.Text = "";
            masktungay.Text = "";
            maskdenngay.Text = "";
            giokh.Text = null;
            txtthoigianchay.Text = "";
            txttimkiem.Text = "";
            ckhuychuyen.Checked = false;
        }

        #region Điều khiển đóng/mở form
        private DieuKhien _Commands;
        /// <summary>
        /// Gets or sets the commands associated with the control.
        /// </summary>
        public DieuKhien Commands
        {
            get { return _Commands; }
            set
            {
                if (value != _Commands)
                {
                    DieuKhien oldValue = _Commands;
                    _Commands = value;
                    OnCommandsChanged(oldValue, value);
                }
            }
        }
        /// <summary>
        /// Called when Commands property has changed.
        /// </summary>
        /// <param name="oldValue">Old property value</param>
        /// <param name="newValue">New property value</param>
        protected virtual void OnCommandsChanged(DieuKhien oldValue, DieuKhien newValue)
        {
            if (newValue != null)
            {
                btndong.Command = newValue.dongfrmcnlc;

            }
            else
            {
                btndong.Command = null;

            }
        }

        #endregion

        private void Loadfrm()
        {
            List<lichchay> lichchay = cnlc.laydslichchay();
            advTree1.Nodes.Clear();
            foreach (var row in lichchay)
            {
                advTree1.Nodes.Add(CreatenoidenRow(row.tuyendg, row.xe, row.ngaykh, row.giokh, row.thoigianchay, row.huychuyen));
            }
        }

        private Node CreatenoidenRow(string tuyen, string xe, string ngay, string gio, string tgchay, string huy)
        {
            Node node = new Node(tuyen);
            node.Cells.Add(new Cell(xe));
            node.Cells.Add(new Cell(ngay));
            node.Cells.Add(new Cell(gio));
            node.Cells.Add(new Cell(tgchay));
            node.Cells.Add(new Cell(huy));
            return node;
        }


        private void checkBoxX1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxX1.Checked)
            {
                maskdenngay.Enabled = true;
                masktungay.Enabled = true;
                maskngayad.Enabled = false;
            }
            else
            {
                maskdenngay.Enabled = false;
                masktungay.Enabled = false;
                maskngayad.Enabled = true;
            }
        }

        private void advTree1_AfterNodeSelect(object sender, AdvTreeNodeEventArgs e)
        {
            try
            {
                Node node = e.Node;
                cobtuyenduong.Text = node.Cells[0].Text;
               cobxe.Text = node.Cells[1].Text;
               maskngayad.Text = node.Cells[2].Text.Replace("/","");
               giokh.Text = node.Cells[3].Text;
               txtthoigianchay.Text = node.Cells[4].Text;
               if (node.Cells[5].Text == "X")
                   ckhuychuyen.Checked = true;
               else ckhuychuyen.Checked = false;
            }
            catch (Exception) { }
        }

        public void themsua(bool a)
        {
            cobtuyenduong.Enabled = a;
            cobxe.Enabled = a;
            maskngayad.Enabled = a;
            checkBoxX1.Enabled = a;
            giokh.Enabled = a;
        }
        private void btnthem_Click(object sender, EventArgs e)
        {
            btnsua.Text = "Lưu";
            btnxoa.Text = "Hủy bỏ";
            btnthem.Enabled = false;
            xoatrang();
            themsua(true);
            ckhuychuyen.Enabled = false;
            this.advTree1.AfterNodeSelect -= new DevComponents.AdvTree.AdvTreeNodeEventHandler(this.advTree1_AfterNodeSelect);
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            if (btnsua.Text == "Lưu")
            {
                if ((checkBoxX1.Checked && dk.catngay(maskdenngay.Text) < dk.catngay(masktungay.Text)) || dk.catngay(masktungay.Text) < System.DateTime.Now)
                    MessageBoxEx.Show("Ngày nhập vào không hợp lệ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                {
                    if (!checkBoxX1.Checked)
                    {
                        cnlc.themlichchay(cobtuyenduong.SelectedValue.ToString(), cobxe.SelectedValue.ToString(), dk.catngay(maskngayad.Text), giokh.Value, int.Parse(txtthoigianchay.Text), huy);
                        MessageBoxEx.Show("Lưu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        xoatrang();
                        Loadfrm();
                    }
                    else
                    {
                        int x = 0;
                        DateTime ngay = dk.catngay(masktungay.Text);
                        while (ngay.AddDays(x) <= dk.catngay(maskdenngay.Text))
                        {

                            cnlc.themlichchay(cobtuyenduong.SelectedValue.ToString(), cobxe.SelectedValue.ToString(), ngay.AddDays(x), giokh.Value, int.Parse(txtthoigianchay.Text), huy);
                            //MessageBoxEx.Show(x.ToString());
                            x += 1;
                        }
                        MessageBoxEx.Show("Lưu thành công " + x + " ngày", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        xoatrang();
                        Loadfrm();
                    }
                }
            }
            else
            {
                cnlc.sualichchay(cobtuyenduong.SelectedValue.ToString(), cobxe.SelectedValue.ToString(), dk.catngay(maskngayad.Text), giokh.Value, int.Parse(txtthoigianchay.Text), huy);
                MessageBoxEx.Show("Sửa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                xoatrang();
                Loadfrm();
            }
        }

        private void ckhuychuyen_CheckedChanged(object sender, EventArgs e)
        {
            if (ckhuychuyen.Checked)
                huy = 1;
            else huy = 0;
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            if (btnxoa.Text == "Hủy bỏ")
            {
                btnthem.Enabled = true;
                btnsua.Text = "Sửa";
                btnxoa.Text = "Xóa";
                xoatrang();                
                checkBoxX1.Checked = false;
                ckhuychuyen.Enabled = true;
                themsua(false);
                this.advTree1.AfterNodeSelect += new DevComponents.AdvTree.AdvTreeNodeEventHandler(this.advTree1_AfterNodeSelect);
            }
            else
            {
                DialogResult dr = MessageBoxEx.Show("Bạn muốn xóa lịch chạy này ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                if (dr == DialogResult.Yes)
                {
                    cnlc.xoalichchay(cobtuyenduong.SelectedValue.ToString(), cobxe.SelectedValue.ToString(), dk.catngay(maskngayad.Text), giokh.Value);
                    MessageBoxEx.Show("Xóa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Loadfrm();
                }
            }
        }

        private void txttimkiem_TextChanged(object sender, EventArgs e)
        {
            List<lichchay> lichchay = cnlc.laydslichchay(txttimkiem.Text);
            advTree1.Nodes.Clear();
            foreach (var row in lichchay)
            {
                advTree1.Nodes.Add(CreatenoidenRow(row.tuyendg, row.xe, row.ngaykh, row.giokh, row.thoigianchay, row.huychuyen));
            }
        }

        private void maskngayad_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void masktungay_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void maskdenngay_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtthoigianchay_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }
    }
}
